package insight.sm.controllers;

import insight.sm.dao.IAssignmentsDAO;
import insight.sm.dao.IEquipmentDAO;
import insight.sm.dao.IProjectDAO;
import insight.sm.db.EntityRowMapper;
import insight.sm.delegates.AjaxDelegate;
import insight.sm.pojo.Project;
import insight.sm.pojo.Users;

import java.util.List;

import javax.activation.DataSource;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class GridController extends GenericController {
	@Autowired
	private JavaMailSenderImpl javaMailSenderImpl;
	
//	@Autowired
//	private IProjectDAO projectDAO;
//	
	@Autowired
	private JdbcTemplate jdbcTemplate;;
	
//	@Autowired
//	private DataSource dataSource;
//	
//	@Autowired
//	private IEquipmentDAO equipmentDAO;
//	
//	@Autowired
//	private IAssignmentsDAO assignmentsDAO;
//	
	@RequestMapping("/getdataPage.htm")
	public ModelAndView getdataPage(HttpServletRequest request,
			HttpServletResponse response) {
		String whereClause = "";
		if (CommonFunc.analyzeRequest(request).length() > 0)
			whereClause = "\" where " + CommonFunc.analyzeRequest(request)
					+ "\" ";
		else
			whereClause = "''";

		int take = 0;
		int skip = 0;
		int pageNo = 0;
		int pageSize = 0;
		String dir = "";
		String field = "";
		List<Users> list = null;
		try {
			take = Integer.parseInt(request.getParameter("take"));
		} catch (Exception e) {
			take = 10;
		}
		try {
			skip = Integer.parseInt(request.getParameter("skip"));
		} catch (Exception e) {
			skip = 0;
		}
		try {
			pageNo = Integer.parseInt(request.getParameter("page"));
		} catch (Exception e) {
			pageNo = 1;
		}
		try {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		} catch (Exception e) {
			pageSize = 10;
		}

		dir = request.getParameter("sort[0][dir]");
		field = request.getParameter("sort[0][field]");
		// List<Users> list1=jdbcTemplate.query("select * from users", new
		// BeanPropertyRowMapper<Users>(Users.class));
		List<Users> list1 = jdbcTemplate.query("select * from users",
				new BeanPropertyRowMapper<Users>(Users.class));
		if (field != null && !field.isEmpty()) {
			System.out.println("call new_procedureFilterDemo ('users',"
					+ whereClause + ", '" + field + "','" + dir + "'," + take
					+ "," + skip + "," + pageNo + "," + pageSize + ")");
			list = jdbcTemplate.query("call new_procedureFilterDemo ('users',"
					+ whereClause + ", '" + field + "','" + dir + "'," + take
					+ "," + skip + "," + pageNo + "," + pageSize + ")",
					new EntityRowMapper<Users>(Users.class));
		} else {
			System.out.println("call new_procedureFilter ('users',"
					+ whereClause + "," + take + "," + skip + "," + pageNo
					+ "," + pageSize + ")");
			list = jdbcTemplate.query("call new_procedureFilter ('users',"
					+ whereClause + "," + take + "," + skip + "," + pageNo
					+ "," + pageSize + ")", new EntityRowMapper<Users>(
					Users.class));
		}

		// if(field!=null && !field.isEmpty())
		// list = jdbcTemplate.query(
		// "call new_procedureDemo ('users','"+field+"','"+dir+"',"+take+","+skip+","+pageNo+","+pageSize+")",
		// new EntityRowMapper<Users>(Users.class));
		// else
		// list = jdbcTemplate.query(
		// "call new_procedure ('users',"+take+","+skip+","+pageNo+","+pageSize+")",
		// new EntityRowMapper<Users>(Users.class));
		try {
			JSONObject jObj = new JSONObject();
			jObj.put("total", list1.size());
			JSONArray jArray = new JSONArray();
			int i = skip + 1;
			for (Users userObj : list) {
				jArray.put(new JSONObject().put("id", i++)
						.put("fullName", userObj.getFullName())
						.put("emailid", userObj.getEmailid())
						.put("username", userObj.getUsername())
						.put("signatureDocId", userObj.getSignatureDocId()));
			}

			jObj.put("data", jArray);
			AjaxDelegate.setResponse(jObj, response);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/getdataPage1.htm")
	public ModelAndView getdataPage1(HttpServletRequest request,
			HttpServletResponse response) {
		String whereClause = "";
		if (CommonFunc.analyzeRequest(request).length() > 0)
			whereClause = "\" where " + CommonFunc.analyzeRequest(request)
					+ "\" ";
		else
			whereClause = "''";

		int take = 0;
		int skip = 0;
		int pageNo = 0;
		int pageSize = 0;
		String dir = "";
		String field = "";
		List<Project> list = null;
		try {
			take = Integer.parseInt(request.getParameter("take"));
		} catch (Exception e) {
			take = 10;
		}
		try {
			skip = Integer.parseInt(request.getParameter("skip"));
		} catch (Exception e) {
			skip = 0;
		}
		try {
			pageNo = Integer.parseInt(request.getParameter("page"));
		} catch (Exception e) {
			pageNo = 1;
		}
		try {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		} catch (Exception e) {
			pageSize = 10;
		}

		dir = request.getParameter("sort[0][dir]");
		field = request.getParameter("sort[0][field]");
		List<Project> list1 = jdbcTemplate.query("select * from project",
				new BeanPropertyRowMapper<Project>(Project.class));
		if (field != null && !field.isEmpty()) {
			System.out.println("call new_procedureFilterDemo ('project',"
					+ whereClause + ", '" + field + "','" + dir + "'," + take
					+ "," + skip + "," + pageNo + "," + pageSize + ")");
			list = jdbcTemplate.query(
					"call new_procedureFilterDemo ('project'," + whereClause
							+ ", '" + field + "','" + dir + "'," + take + ","
							+ skip + "," + pageNo + "," + pageSize + ")",
					new EntityRowMapper<Project>(Project.class));
		} else {
			System.out.println("call new_procedureFilter ('project',"
					+ whereClause + "," + take + "," + skip + "," + pageNo
					+ "," + pageSize + ")");
			list = jdbcTemplate.query("call new_procedureFilter ('project',"
					+ whereClause + "," + take + "," + skip + "," + pageNo
					+ "," + pageSize + ")", new EntityRowMapper<Project>(
					Project.class));
		}

		// if(field!=null && !field.isEmpty())
		// list = jdbcTemplate.query(
		// "call new_procedureDemo ('project','"+field+"','"+dir+"',"+take+","+skip+","+pageNo+","+pageSize+")",
		// new EntityRowMapper<Project>(Project.class));
		// else
		// list = jdbcTemplate.query(
		// "call new_procedure ('project',"+take+","+skip+","+pageNo+","+pageSize+")",
		// new EntityRowMapper<Project>(Project.class));
		try {
			JSONObject jObj = new JSONObject();
			jObj.put("total", list1.size());
			JSONArray jArray = new JSONArray();
			int i = skip + 1;
			for (Project prjObj : list) {
				System.out.println(prjObj.toString());
				jArray.put(new JSONObject().put("id", i++)
						.put("projectId", prjObj.getProjectId())
						.put("description", prjObj.getDescription())
						.put("projectName", prjObj.getProjectName())
						.put("location", prjObj.getLocation()));
			}

			jObj.put("data", jArray);
			AjaxDelegate.setResponse(jObj, response);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	@RequestMapping("/mail.htm")
	public ModelAndView sendMail(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("serverTime", "10:00pm");
		for(int i=0;i<=20;i++)
			sendDemoMail();
		return null;
	}
	
	

	private synchronized void sendDemoMail() {
		MimeMessage message=javaMailSenderImpl.createMimeMessage();
		MimeMessageHelper helper=new MimeMessageHelper(message);
		try {
			helper.setFrom("sitemansomething@gmail.com");
			helper.setTo("bisarga2000@gmail.com");
			helper.setSubject("test subject");
			helper.setText("subject body");
			javaMailSenderImpl.send(message);
			System.out.println("Mail sent ");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
